package com.wd.tech.adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.wd.tech.R;
import com.wd.tech.bean.FindCommunityListBean;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommunityAdapter extends RecyclerView.Adapter<CommunityAdapter.MyViewHolder> {
    FragmentActivity activity;
    List<FindCommunityListBean.ResultBean> list;

    public CommunityAdapter(FragmentActivity activity, List<FindCommunityListBean.ResultBean> list) {
        this.activity = activity;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LinearLayout.inflate(parent.getContext(), R.layout.community_adapter, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //用户名称
        String nickName = list.get(position).getNickName();
        holder.nameCommunity.setText(nickName);
        String signature = list.get(position).getSignature();
        holder.signatureCommunity.setText(signature);
        //帖子内容
        String content = list.get(position).getContent();
        holder.moreCommunity.setText(content);
        //用户圆形头像
        Glide.with(holder.headCommunity)
                .load(list.get(position).getHeadPic())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.headCommunity);
        //帖子内容图片
        String file = list.get(position).getFile();
        if (file != null && !file.equals("")) {
            holder.img1Community.setVisibility(View.VISIBLE);
            Glide.with(holder.img1Community)
                    .load(file)
                    .into(holder.img1Community);
        } else {
            holder.img1Community.setVisibility(View.GONE);
        }
        //帖子发布时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM:dd HH:mm");
        String format = simpleDateFormat.format(list.get(position).getPublishTime());
        holder.timeCommunity.setText(format);

        int whetherGreat = list.get(position).getWhetherGreat();
        if (whetherGreat==1){
            holder.dianzan.setImageResource(R.drawable.quxiaodianzan_c);
        }else {
            holder.dianzan.setImageResource(R.drawable.dianzan_c);
        }

        holder.dianzanText.setText(list.get(position).getPraise()+"");

        holder.dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setonClickLinster != null) {
                    setonClickLinster.onClick(position);
                }
            }
        });

        holder.pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.head_community)
        ImageView headCommunity;
        @BindView(R.id.name_community)
        TextView nameCommunity;
        @BindView(R.id.signature_community)
        TextView signatureCommunity;
        @BindView(R.id.time_community)
        TextView timeCommunity;
        @BindView(R.id.button_show_communty)
        Button buttonShowCommunty;
        @BindView(R.id.button_buddy_communty)
        Button buttonBuddyCommunty;
        @BindView(R.id.more_community)
        TextView moreCommunity;
        RelativeLayout showMore;
        @BindView(R.id.img1_community)
        ImageView img1Community;
        @BindView(R.id.dianzan)
        ImageView dianzan;
        @BindView(R.id.dianzan_text)
        TextView dianzanText;
        @BindView(R.id.pinglun)
        ImageView pinglun;
        @BindView(R.id.pinglun_text)
        TextView pinglunText;
        @BindView(R.id.fenxiang)
        ImageView fenxiang;
        @BindView(R.id.fenxiang_text)
        TextView fenxiangText;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    setonClickLinster setonClickLinster;

    public void setSetonClickLinster(CommunityAdapter.setonClickLinster setonClickLinster) {
        this.setonClickLinster = setonClickLinster;
    }

    public interface setonClickLinster {
        void onClick(int position);
    }
}
